package com.itshidu.game.service.impl

import com.itshidu.game.entity.FightLog
import com.itshidu.game.entity.Hero
import com.itshidu.game.mapper.FightLogMapper
import com.itshidu.game.mapper.HeroMapper
import com.itshidu.game.service.FightLogService
import com.itshidu.game.service.FightService
import net.sf.ehcache.util.ratestatistics.RateStatistic
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

@Service
class FightServiceImpl implements FightService {


    @Autowired HeroMapper heroMapper
    @Autowired FightLogMapper fightLogMapper

    @Override
    String fight(Hero h1, Hero h2,long seed) {
        String.metaClass.mod = { List list ->
            String.format(delegate, list as Object[])
        }
        //long seed = new Date().getTime()
        Random random = new Random(seed)
        for (i in 0..<10) {
            println random.nextDouble();
        }

        // 初始化属性
        h1._hp = h1.hp
        h1._mp = h1.mp
        h2._hp = h2.hp
        h2._mp = h2.mp

        def ret = ''

        //ret += '命中率：[%s:%.2f]|[%s:%.2f]<hr>'%[h1.name,hit(h1.hit,h2.dodge),h2.name,hit(h2.hit,h1.dodge)]

        // 计算谁先手
        def a = h1;
        def b = h2;
        if (h2.agile > h1.agile) {
            a = h2;
            b = h1;
            ret += '【%s】敏捷更高，优先出手。'%[a.name]
        }else if (h2.agile == h1.agile) {
            ret += '双方敏捷相同，'
            if (h2.level > h1.level) {
                a = h2;
                b = h1;
            }
            ret += '【%s】等级更高，优先出手。' % [a.name]
        } else {
            ret += '【%s】敏捷更高，优先出手。'%[a.name]
        }
        ret += '<hr>'

        while (a._hp > 0 && b._hp > 0) {
            println('-----------------')
            // a 攻击 b

            ret += ('【%s】对【%s】发动【普通攻击】，上去就是一拳，'%[a.name, b.name])

            def hitValue = hit(a.hit,b.dodge)
            if (random.nextDouble() > hitValue) {
                ret += '这一击虽然凌厉，但只见【%s】身形闪动，竟然【闪避】了。' % [b.name]
            } else {
                int damage = (a.atk - b.armor)

                /* 是否暴击 */
                double b1 = crit(a.critRate)
                double b2 = random.nextDouble()
                println(b1)
                println(b2)
                if (b1 >= b2) {
                    damage *= (1.5+(a.critVal*0.02))
                    ret += '竟是【暴击】，'
                }

                b._hp -= damage
                ret += ('造成 %d 伤害。' % [damage])
            }

            ret += '<br>---------------------<br>'
            ret += '[%s:%d] | [%s:%d]'%[h1.name,h1._hp,h2.name,h2._hp]
            ret += '<br>---------------------<br>'

            ret += '<br><br>'

            def c = a
            a = b
            b = c

        }

        def winner = a
        if (b._hp > 0) {
            winner = b
        }

        ret += '<span style="color: #ff4d00">'
        ret += '【%s】获胜！！！'%[winner.name]
        ret += '</span>'

        //如果胜利者是挑战者
        if (winner.id == h1.id) {


            int cj = (h2.level-h1.level) //等级差距
            int exp = 3000*(1+cj*0.15) //每级差15%经验
            if (exp < 100) {
                exp = 100 //保底100经验
            }
            winner.exp += exp

            heroMapper.updateById(winner)
            ret += '<br>[%s]挑战成功，经验+%d<br>'%[winner.name,exp]
        }

        def fightLog = new FightLog()
        fightLog.created = new Date()
        fightLog.heroId = h1.id
        fightLog.targetId = h2.id
        fightLog.winnerId = winner.id
        fightLog.seed = seed
        fightLogMapper.insert(fightLog)

        return ret
    }

    static def hit(m, n) {
        def a = 30
        def hit = (m+a)/(n+a)*0.9
        return hit
    }

    static double crit(int rate) {
        int a = 50 //多少级可以到50%
        double r = a/(a+rate)*0.9
        return (1-r)
    }
    public static void main(String[] args) {
        String.metaClass.mod = { List list ->
            String.format(delegate, list as Object[])
        }

        Random r = new Random(65536)
        for (i in 0..<10) {
            println(r.nextDouble())

        }


    }
}
